<template>
  <div style="width: 100%;height: 500px;padding-top:0;margin: 0 ">
    <div class="pageheader">
      <div class="pageheader-1">
        <i class="el-icon-user-solid"></i><label>家庭档案管理</label>
      </div>
      <div style="height: 100%;position: absolute;top: 20px;left: 350px">
        <el-form :model="input" ref="key">
        <el-input
          style="width: 200px;position: absolute;top: 5px;left: 0px"
          size="small"
          placeholder="请输入家庭编号"
          suffix-icon="el-icon-search"
          maxlength="20"
          v-model="input.input3">
        </el-input>
        <el-button type="primary" class="searchbtn" @click="find('key')" plain>查询</el-button>
        </el-form>
        <el-button type="primary" v-show="list[1].children[0].showadd" @click="dialogFormVisible=true" class="add">添加</el-button>
        <!--添加表单-->
        <el-dialog title="添加" @close="this.reload" :visible.sync="dialogFormVisible">
          <el-form :rules="rules" :model="form" ref="addAction">
            <el-form-item prop="familyid" label="家庭编号" :label-width="formLabelWidth">
            <el-input v-model="form.familyid" autocomplete="off"></el-input>
          </el-form-item>
            <el-form-item prop="handname" label="户主姓名" :label-width="formLabelWidth">
              <el-input v-model="form.handname" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="handid" label="身份证号" :label-width="formLabelWidth">
              <el-input v-model="form.handid" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item  prop="village" label="所在村名" :label-width="formLabelWidth">
              <el-select v-model="form.village" placeholder="请选择所在村">
                <el-option  v-for="item in area" :key="item" :label="item.areaname" :value="item.areaid"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="add ('addAction')">确 定</el-button>
          </div>
        </el-dialog>
        <!--修改表单-->
        <el-dialog title="修改" :visible.sync="editdep">
          <el-form :rules="rules" :model="e_form" ref="editdep1">
            <el-form-item prop="familyid" label="家庭编号" :label-width="formLabelWidth">
              <el-input :disabled="true" v-model="e_form.familyid" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="handname" label="户主姓名" :label-width="formLabelWidth">
              <el-input v-model="e_form.handname" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="handid" label="身份证号" :label-width="formLabelWidth">
              <el-input v-model="e_form.handid" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item  prop="village" label="所在村名" :label-width="formLabelWidth">
              <el-select v-model="e_form.village" placeholder="请选择所在村">
                <el-option  v-for="item in area" :key="item" :label="item.areaname" :value="item.areaid"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="editdep = false">取 消</el-button>
            <el-button type="primary" @click="edit ('editdep1')">确 定</el-button>
          </div>
        </el-dialog>
        <!--提示 -->
        <el-dialog
          title="提示"
          :visible.sync="openelete"
          width="30%"
          center>
          <span>你确定要删除该条记录吗?</span>
          <span slot="footer" class="dialog-footer">
    <el-button @click="openelete = false">取 消</el-button>
    <el-button type="primary" @click="delete_data" >确 定</el-button>
  </span>
        </el-dialog>
        <!--家庭成员信息 -->
        <el-dialog title="家庭成员信息" @close="this.reload" :visible.sync="Tpeple" width="100%" >
          <el-button style="margin: 0px 40px 10px" v-show="list[1].children[0].findadd" type="primary" @click="opencheng = true">添加成员</el-button>
          <el-table
            :data="TData"
            border
            style="width: 100%;height: 500px">
            <el-table-column
              prop="nongid"
              width="100"
              label="农合证号">
            </el-table-column>
            <el-table-column
              prop="chengname"
              width="80"
              label="姓名">
            </el-table-column>
            <el-table-column
              prop="chengid"
              label="身份证号">
            </el-table-column>
            <el-table-column
              prop="sex"
              width="55"
              label="性别">
            </el-table-column>
            <el-table-column
              prop="age"
              width="55"
              label="年龄">
            </el-table-column>
            <el-table-column
              prop="guanxi"
              width="110"
              label="与户主关系">
            </el-table-column>
            <el-table-column
              prop="housts"
              label="家庭住址">
            </el-table-column>
            <el-table-column
              prop="tel"
              width="120"
              label="联系方式">
            </el-table-column>
            <el-table-column
              width="100"
              prop="resig"
              label="参合发票">
            </el-table-column>
            <el-table-column
              fixed="right"
              label="操作">
              <template slot-scope="scope">
                <el-button type="primary" v-show="list[1].children[0].findedit" @click="o_editpeple(scope.row)">修改</el-button>
                <el-button type="danger" v-show="list[1].children[0].finddelete" @click="o_deletepeple(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-dialog>
        <!--添加成员信息-->
        <el-dialog title="添加" @close="this.reload" :visible.sync="opencheng">
          <el-form :rules="rules" :model="pepleform" ref="addpeple">
            <el-form-item prop="chengname" label="姓名" :label-width="formLabelWidth">
              <el-input v-model="pepleform.chengname" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="sex" label="性别" :label-width="formLabelWidth">
              <el-input v-model="pepleform.sex" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="age" label="年龄" :label-width="formLabelWidth">
              <el-input v-model="pepleform.age" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="chengid" label="身份证号" :label-width="formLabelWidth">
              <el-input v-model="pepleform.chengid" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="nongid" label="农合证号" :label-width="formLabelWidth">
              <el-input v-model="pepleform.nongid" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="guanxi" label="与户主关系" :label-width="formLabelWidth">
              <el-input v-model="pepleform.guanxi" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="housts" label="家庭住址" :label-width="formLabelWidth">
              <el-input v-model="pepleform.housts" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item prop="tel" label="联系方式" :label-width="formLabelWidth">
              <el-input v-model="pepleform.tel" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="opencheng = false">取 消</el-button>
            <el-button type="primary" @click="addpeple ('addpeple')">确 定</el-button>
          </div>
        </el-dialog>
        <!--修改成员信息-->
        <el-dialog title="修改" @close="this.reload" :visible.sync="eopencheng">
          <el-form :rules="rules" :model="pepleform" ref="editpeple">
            <el-form-item prop="chengname" label="姓名" :label-width="formLabelWidth">
              <el-input v-model="pepleform.chengname" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="sex" label="性别" :label-width="formLabelWidth">
              <el-input v-model="pepleform.sex" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="age" label="年龄" :label-width="formLabelWidth">
              <el-input v-model="pepleform.age" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="chengid" label="身份证号" :label-width="formLabelWidth">
              <el-input v-model="pepleform.chengid" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="nongid" label="农合证号" :label-width="formLabelWidth">
              <el-input v-model="pepleform.nongid" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="guanxi" label="与户主关系" :label-width="formLabelWidth">
              <el-input v-model="pepleform.guanxi" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="housts" label="家庭住址" :label-width="formLabelWidth">
              <el-input v-model="pepleform.housts" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="resig" label="参合发票号" :label-width="formLabelWidth">
              <el-input v-model="pepleform.resig" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="chantime" label="参合时间" :label-width="formLabelWidth">
              <el-input v-model="pepleform.chantime" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="tel" label="联系方式" :label-width="formLabelWidth">
              <el-input v-model="pepleform.tel" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="eopencheng = false">取 消</el-button>
            <el-button type="primary" @click="editpeple ('editpeple')">确 定</el-button>
          </div>
        </el-dialog>
      </div>

    </div>
    <el-table
      :data="tableData"
      border
      style="width: 100%;height: 80%">

      <el-table-column
        prop="familyid"
        width="120"
        label="家庭编号">
      </el-table-column>
      <el-table-column
        prop="handname"
        width="120"
        label="户主姓名">
      </el-table-column>
      <el-table-column
        prop="handid"
        label="身份证号">
      </el-table-column>
      <el-table-column
        prop="village"
        label="所在村编号">
      </el-table-column>
      <el-table-column
        prop="creattime"
        label="建档时间">
      </el-table-column>
      <el-table-column
        width="100"
        label="成员">
        <template slot-scope="scope">
          <el-button type="primary" v-show="list[1].children[0].find" @click="openpeple(scope.row)" >查看</el-button>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        width="180"
        label="操作">
        <template slot-scope="scope">
          <el-button v-show="list[1].children[0].showedit" type="primary" @click="handleClick(scope.row)" >修改</el-button>
          <el-button v-show="list[1].children[0].showdelete" type="danger" @click="opendelete(scope.row)" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页 -->
    <div class="pagination">
      <el-pagination
        background
        layout="prev, pager, next"
        page-size="5"
        :total="total"
        @current-change="page">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  inject: ['reload', 'list'],
  methods: {
    // 查询功能
    find (key) {
      const _this = this
      if (this.input.input3 !== '') {
        axios.get('http://localhost:8088/findByFamilyidF/' + this.input.input3).then(function (resp) {
          _this.tableData = resp.data
          _this.total = 1
        })
      } else {
        this.reload()
      }
    },
    // 添加
    add (addAction) {
      const _this = this
      this.$refs[addAction].validate((valid) => {
        axios.get('http://localhost:8088/findByFamilyidF/' + this.form.areaid).then(function (resp) {
          if (resp.data === '') {
            axios.post('http://localhost:8088/saveFamily', _this.form).then(function (resp) {
              _this.$message({
                message: '添加成功！',
                type: 'success'
              })
              _this.reload()
            })
          } else {
            alert('编号已经存在，请重新输入！')
          }
        })
      })
    },
    // 修改
    edit (editdep1) {
      this.$refs[editdep1].validate((valid) => {
        axios.post('http://localhost:8088/saveFamily', this.e_form).then(function (resp) {
        })
      })
      this.$message({
        message: '修改成功！',
        type: 'success'
      })
      this.reload()
    },
    // 读取当前行的数据
    handleClick (row) {
      console.log(row)
      this.e_form = row
      this.editdep = true
    },
    // 删除
    opendelete (row) {
      this.openelete = true
      this.form = row
    },
    delete_data () {
      axios.post('http://localhost:8088/deleteFamily/' + this.form.id + '/' + this.form.familyid).then(function (resp) {
      })
      this.$message({
        message: '删除成功！',
        type: 'success'
      })
      this.reload()
    },
    // 分页模块
    page (currentpage) {
      const _this = this
      axios.get('http://localhost:8088/findFamilyAll/' + currentpage + '/5').then(function (resp) {
        console.log(resp.data)
        _this.tableData = resp.data.content
        _this.total = resp.data.totalElements
      })
    },
    openpeple (row) {
      const _this = this
      this.form = row
      axios.get('http://localhost:8088/findByPeple/' + row.familyid).then(function (resp) {
        _this.TData = resp.data
      })
      this.Tpeple = true
    },
    // 添加家庭成员
    addpeple (addpeple) {
      this.pepleform.familyid = this.form.familyid
      this.$refs[addpeple].validate((valid) => {
        axios.post('http://localhost:8088/savePeples', this.pepleform).then(function (resp) {
        })
      })
      this.reload()
    },
    // 修改成员信息
    o_editpeple (row) {
      this.pepleform = row
      this.eopencheng = true
    },
    editpeple (editpeple) {
      this.$refs[editpeple].validate((valid) => {
        axios.post('http://localhost:8088/savePeples', this.pepleform).then(function (resp) {
        })
      })
      this.reload()
    },
    o_deletepeple (row) {
      axios.post('http://localhost:8088/deletePeple/' + row.chengid).then(function (resp) {
      })
      this.reload()
    }
  },
  // 接受后台数据
  created () {
    const _this = this
    axios.get('http://localhost:8088/findByLevel').then(function (resp) {
      _this.area = resp.data
    })
    axios.get('http://localhost:8088/findFamilyAll/1/5').then(function (resp) {
      _this.tableData = resp.data.content
      _this.total = resp.data.totalElements
    })
  },
  //  数据绑定
  data () {
    return {
      total: null,
      input: {
        input3: ''
      },
      tableData: null,
      dialogFormVisible: false,
      editdep: false,
      openelete: false,
      area: null,
      Tpeple: false,
      TData: null,
      form: {
        id: '',
        familyid: '',
        handname: '',
        handid: '',
        village: '',
        creattime: ''
      },
      e_form: {
        id: '',
        familyid: '',
        handname: '',
        handid: '',
        village: '',
        creattime: ''
      },
      formLabelWidth: '100px',
      rules: {
        familyid: [
          { required: true, message: '请输入家庭编号', trigger: 'blur' }
        ],
        handname: [
          { required: true, message: '请输入户主姓名', trigger: 'blur' }
        ],
        handid: [
          { required: true, message: '请输入身份证号', trigger: 'blur' }
        ],
        village: [
          { required: true, message: '请选择所在的村', trigger: 'blur' }
        ],
        age: [
          { required: true, message: '请输入成员年龄', trigger: 'blur' }
        ],
        chantime: [
          { required: true, message: '请输入参合时间', trigger: 'blur' }
        ],
        chengid: [
          { required: true, message: '请输入身份证号', trigger: 'blur' }
        ],
        chengname: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        guanxi: [
          { required: true, message: '与户主关系不能为空', trigger: 'blur' }
        ],
        housts: [
          { required: true, message: '请填写家庭住址', trigger: 'blur' }
        ],
        nongid: [
          { required: true, message: '农合证号不能为空', trigger: 'blur' }
        ],
        resig: [
          { required: true, message: '请输入参合发票号', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请输入性别', trigger: 'blur' }
        ],
        tel: [
          { required: true, message: '请填写联系方式', trigger: 'blur' }
        ]
      },
      pepleform: {
        id: '',
        age: '',
        chantime: '未参合',
        chengid: '',
        chengname: '',
        familyid: '',
        guanxi: '',
        housts: '',
        nongid: '',
        resig: '未参合',
        sex: '',
        tel: ''
      },
      opencheng: false,
      eopencheng: false
    }
  }
}
</script>
